package com.btt.pattern.prototype.deep;

import java.io.*;
import java.util.Date;

/**
 * 深克隆-->利用序列化方式传给克隆对象，只是将值赋值给克隆对象，
 * 并不是将引用传给克隆对象
 *
 * created by Btt on 2018/6/28
 */

public class QiTianDaSheng extends Monkey implements Cloneable, Serializable {

    private JinGuBang jinGuBang;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return this.deepClone();
    }

    public Object deepClone(){
        try {
            //序列化方式传给copy对象
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            oos.flush();
            oos.close();
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            QiTianDaSheng copy = (QiTianDaSheng)ois.readObject();
            ois.close();
            copy.setBirthday(new Date());
            return copy;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;

    }

    public QiTianDaSheng(){
        this.setBirthday(new Date());
        this.jinGuBang = new JinGuBang();
    }

    public JinGuBang getJinGuBang() {
        return jinGuBang;
    }

    public void setJinGuBang(JinGuBang jinGuBang) {
        this.jinGuBang = jinGuBang;
    }
}
